package utils;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

public class jdbc {
    private static DruidDataSource druiddatasource;
    static {
        Properties properties=new Properties();
        InputStream inputstream =jdbc.class.getClassLoader().getResourceAsStream("jdbc.properties");
        try {
            properties.load(inputstream);
            druiddatasource=(DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     *
     * @return 数据库连接池对象
     * @throws SQLException
     */
    public static Connection getconnection() throws SQLException {
        Connection conn=null;
        conn=druiddatasource.getConnection();
        return conn;
    }

    /**
     * 关闭；连接
     * @param connection
     * @throws SQLException
     */
    public static void close(Connection connection) throws SQLException {
        if (connection!=null){
            connection.close();
        }
    }
}
